package com.dingpeikeji.quickdeer.modules.wx.air;

import java.io.IOException;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.modules.cupons.entity.CuponsInfo;
import com.dingpeikeji.quickdeer.core.modules.cupons.service.TCuponsService;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.service.OfficeService;
import com.dingpeikeji.quickdeer.mobile.service.OrderService;
import com.dingpeikeji.quickdeer.modules.base.service.FrontUserService;
import com.dingpeikeji.quickdeer.modules.base.web.FSysController;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.DemandsReact;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.ProRentOrders;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotypeForsale;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TDemands;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroup;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.FDemandsService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.FRentproductService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.RentproductOrderService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentgroupService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentproductService;
import com.dingpeikeji.quickdeer.utils.BackResult;
import com.dingpeikeji.quickdeer.utils.StringUtil;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("${wxPath}/air")
public class WxAirController {
	@Autowired
	private TCargotypeService tCargotypeService;
	@Autowired
	private TOrdersService tOrdersService;
	@Autowired
	private FrontUserService frontUserService;
	@Autowired
	private RentproductOrderService rentproductOrderService;
	@Autowired
	private TSitedtlService tSitedtlService;
	@Autowired
	private FRentproductService rentproductService;
	@Autowired
	private OfficeService officeService;
	/**
	 * 接送机
	 */
	@RequestMapping("airlist")
	public String airList(TRentproduct tRentproduct, HttpServletRequest request, 
			HttpServletResponse response, Model model,TCargotypeForsale cargotypeForsale)
					throws Exception {
		String mes = request.getParameter("mes");
		String destSiteName = request.getParameter("destcity.sitename");
		String detailAddress = request.getParameter("aboardAddress");
		if (StringUtils.isEmpty(mes)) {//没有搜索结果返回页面
			return "wx/modules/airProduct/airResult";
		}
		tRentproduct.setRemarks(mes);
		//获取车型
		TCargotype cargotype = new TCargotype();
		String tstyle = request.getParameter("tstyle");
		cargotypeForsale.setCargotype(cargotype);
		cargotypeForsale.getCargotype().setTstyles(tstyle);
		//百度地图自动赋值城市名称,根据名称查询
		TSitedtl sitedtl = tSitedtlService.getDestCityByName(tRentproduct.getDestcity().getSitename());
		if (sitedtl!=null) {
			tRentproduct.setDestcity(sitedtl);//不匹配,重新赋值
			cargotypeForsale.setDepartcity(tRentproduct.getDepartcity());
			cargotypeForsale.setDestcity(tRentproduct.getDestcity());
		}else{
			tRentproduct.setDestcity(null);//查询不到目的地
			cargotypeForsale.setDepartcity(tRentproduct.getDepartcity());
			cargotypeForsale.setDestcity(tRentproduct.getDestcity());
		}
		//产品名称
		String productName = "";
		if (tRentproduct.getDestcity()!=null) {
			productName = tRentproduct.getDepartcity().getSitename()
					+"-"+tRentproduct.getDestcity().getSitename()+"("+tRentproduct.getTabType()+")";
		}else{
			productName = tRentproduct.getDepartcity().getSitename()
					+"-"+destSiteName+"("+tRentproduct.getTabType()+")";
		}
		tRentproduct.setProductname(productName);
		tRentproduct.setAboardAddress(detailAddress);
		String kms = tRentproduct.getMapkm();//地图测算的距离
		double distance = 0.0;
		int disLevel = 0;
		if (StringUtils.isNotEmpty(kms)) {
			if (kms.indexOf("公里")!=-1) {
				distance = Double.parseDouble(kms.substring(0, kms.indexOf("公里")));
				if (distance<=50&&distance>=0) {
					disLevel = 1;
				}else if (distance<=150&&distance>50) {
					disLevel = 2;
				}else if (distance<=275&&distance>150) {
					disLevel = 3;
				}else if (distance<=500&&distance>275) {
					disLevel = 4;
				}else {
					disLevel = 5;
				}
			}else{
				disLevel = 1;//距离太近,取不到公里数
			}
		}
		
		//根据出发地,目的地 查询所有车型
		Page<TCargotypeForsale> page = new Page<TCargotypeForsale>(request, response);
		page.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));
		cargotypeForsale.setPage(page);
		List<TCargotypeForsale> cargotypeList = rentproductService.findAirCargotypeList(cargotypeForsale);
		page.setList(cargotypeList);
		List<Office> officeList = null;
		//根据车型获取对应的车队
		if (cargotypeList.size()>0&&cargotypeList!=null) {
			for (TCargotypeForsale tc : cargotypeList) {
				double condition0 = ((tc.getCargotype().getNudePrice()/2.00+100)+
						(tc.getCargotype().getOilcost()+tc.getCargotype().getRoadmodules())*distance*2);
				double condition1 = (tc.getCargotype().getNudePrice()+
						(tc.getCargotype().getOilcost()+tc.getCargotype().getRoadmodules())*distance*2);
				double condition2 = (((tc.getCargotype().getNudePrice()+300)*(distance/500+1))+
						(tc.getCargotype().getOilcost()+tc.getCargotype().getRoadmodules())*distance*2);

				Set<Double> priceSet = new TreeSet<Double>();
				List<Double> priceList = new ArrayList<Double>();
				tc.setDepartcity(tRentproduct.getDepartcity());
				tc.setDestcity(tRentproduct.getDestcity());
				tc.setDisLevel(disLevel);//距离等级
				officeList = rentproductService.findAirProductByType(tc);
				if (officeList.size()>0&&officeList!=null) {
					for (Office office : officeList) {
						if (distance<=50&&distance>=0) {
							office.setAirPrice(Math.ceil(condition0*office.getFactor()));
						}else if(distance<=500&&distance>50) {
							office.setAirPrice(Math.ceil(condition1*office.getFactor()));
						}else {
							office.setAirPrice(Math.ceil(condition2*office.getFactor()));
						}
						}
				}
				tc.setOfficeList(officeList);
				//取最低价
				for (Office office : officeList) {
					priceSet.add(office.getAirPrice());
				}
				for (Double double1 : priceSet) {
					priceList.add(double1);
				}
				if (priceList.size()>0) {
					tc.setLowestAirPrice(priceList.get(0));
				}
			}
		}
		model.addAttribute("cargotypeList", cargotypeList);
		model.addAttribute("page", page);
		model.addAttribute("tstyle", tstyle);
		model.addAttribute("tRentproduct", tRentproduct);
		return "wx/modules/airProduct/airResult";//modules/rentproduct/fRentproductForTravelList
	}
	@RequestMapping("airbooking")
	public String airbooking(TRentproduct tRentproduct, Model model, HttpServletRequest request
			,HttpServletResponse response) throws Exception {
		//根据车队abbr和name查询车队id
		String id = officeService.findIdByNameAndAbbr(tRentproduct.getOffice().getName(),tRentproduct.getOffice().getAbbr());
		tRentproduct.getOffice().setId(id);
		model.addAttribute("tRentproduct", tRentproduct);
		return "wx/modules/airProduct/airBooking";
	}

}
